/**
 * Created by zhangsong on 2019/9/23.
 */
import globalConfig from '@/constants/config';
import { routerRedux } from 'dva/router';
import { formatMessage } from '@/utils/i18n';
import VaccineReducer from '@/dva/application/vaccine';
import * as vaccineApi from '@/services/application/vaccine';
import Modal from '@/components/Modal';
import { StaffApplication } from '@/constants/routes';
import { defaultPaginationParam } from '@/constants/styleConfig';
// import { mapToSearch } from '@/utils/utils';
import URI from '@/services/uri';
import _ from 'lodash';

const urly = URI.application;

const { Namespace, State, Reducers, Effects } = VaccineReducer;

const Model = {
  namespace: Namespace,
  state: State,
  effects: {
    *[Effects.addVaccine]({ data = {} }, { put, call }) {
      const res = yield call(vaccineApi.addVaccine, data);
      yield put({
        type: Reducers.setState,
        data: {
          saveLoading: true,
        },
      });
      if (res) {
        yield put({
          type: Reducers.setState,
          data: {
            saveLoading: false,
          },
        });
        yield put({
          type: Effects.getVaccine,
          data: data.companyId,
        });
      }
    },
    *[Effects.getVaccine]({ data = {} }, { put, call }) {
      yield put({
        type: Reducers.setState,
        data: {
          saveLoading: true,
        },
      });
      const res = yield call(vaccineApi.getVaccine, data);
      console.log('tamade ', res);
      if (res) {
        yield put({
          type: Reducers.setState,
          data: {
            saveLoading: false,
            vaccinedetial: res.msg.data ? res.msg.data : { disable: 0 },
            modifycode: res.msg.result,
          },
        });
      } else {
        yield put({
          type: Reducers.setState,
          data: {
            saveLoading: false,
            vaccinedetial: null,
          },
        });
      }
    },
    *[Effects.modifyVaccine]({ data = {} }, { put, call }) {
      yield put({
        type: Reducers.setState,
        data: {
          saveLoading: true,
        },
      });
      const res = yield call(vaccineApi.modifyVaccine, data);
      if (res) {
        yield put({
          type: Reducers.setState,
          data: {
            saveLoading: false,
          },
        });
        yield put({
          type: Effects.getVaccine,
          data: data.companyId,
        });
      }
    },
    *[Effects.setData]({ data = {} }, { put, call }) {
      yield put({
        type: Reducers.setState,
        data: {
          partbinfo: {},
        },
      });
    },
    *[Effects.setStateSw]({ data = {} }, { put, call }) {
      const res = data.vaccinedetial;
      yield put({
        type: Reducers.setState,
        data: {
          vaccinedetial: res,
        },
      });
    },
    *[Effects.uploadfile]({ data = {} }, { put, call }) {
      yield put({
        type: Reducers.setState,
        data: {
          saveLoading: true,
        },
      });
      const res = yield call(vaccineApi.uploadPartb, data);
      console.log('上传文件的', data, res);
      if (res.code === 0) {
        Modal.success({
          title: _.get(res, 'msg', ''),
        });
      } else {
        Modal.error({
          title: formatMessage('baseInfo.errorTitle'),
          content: _.get(res, 'msg', ''),
        });
      }
      // const datasou = [...res.failList]
      // for (var i = 0; i < 10; i++) {
      //   datasou.push({
      //     create_time: null,
      //     passNo: "A245245",
      //     projectExpire: "2022-12-12 00:00:00",
      //     result: "搜索不到",
      //     update_time: null,
      //   })
      // }
      // res.failList = datasou
      if (res) {
        yield put({
          type: Reducers.setState,
          data: {
            partbinfo: res,
          },
        });
      }
      yield put({
        type: Reducers.setState,
        data: {
          saveLoading: false,
        },
      });
    },

    *[Effects.downLoadXlsx]({ data }, { put, call }) {

      yield put({
        type: Reducers.setState,
        data: {
          xslloading: true,
        },
      });
      const response = yield call(vaccineApi.downloadPartb, data);
      console.log('这是真的下载；饿', urly.vaccineUrl.download)
      // return;
      // // if (response) {
      // //   const url = window.URL.createObjectURL(new Blob([response]));
      // const link = document.createElement('a'); // 创建a标签
      // //   link.style.display = 'none';
      // link.href = urly.alarm.downxlsx; // 设置a标签路径
      // // link.download = `person—list${moment(data.startTime).format('YYYY-MM-DD')}-${moment(data.endTime).format('YYYY-MM-DD')}.xlsx`; // 设置文件名， 也可以这种写法 （link.setAttribute('download', '名单列表.xls');
      // document.body.appendChild(link);
      // link.click();
      // URL.revokeObjectURL(link.href); // 释放 URL对象
      // document.body.removeChild(link);
      const eleLink = document.createElement('a');
      eleLink.style.display = 'none';
      // eleLink.target = "_blank"
      eleLink.href = `${urly.vaccineUrl.download}`;
      // eleLink.href = record;
      document.body.appendChild(eleLink);
      eleLink.click();
      document.body.removeChild(eleLink);
      yield put({
        type: Reducers.setState,
        data: {
          xslloading: false,
        },
      });
    },
  },
  reducers: {
    [Reducers.setState](state, { data = {} } = {}) {
      console.log('来了吗');
      return {
        ...state,
        ...data,
      };
    },
  },
};

export default Model;
